package com.compmov.socialnetwork;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.compmov.datamodel.Lugar;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private ArrayList<Lugar> mOverlaysLugar = new ArrayList<Lugar>();
	private Context mContext;
	private MapView mv;

	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}

	public MyItemizedOverlay(Context context,Drawable defaultMarker, MapView mv) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		this.mv=mv;
	}

	public void update(){
		populate();
	}

	@Override
	protected boolean onTap(int i) {
		OverlayItem tapItem=getItem(i);
		final GeoPoint tapPoint=tapItem.getPoint();
		final Lugar lugar=mOverlaysLugar.get(i);
		final String address;
		if(i!=0){

			final int j=i;
			OverlayItem currentPosItem=getItem(0);			
			final GeoPoint currentPosPoint=currentPosItem.getPoint();
			final Dialog dialog = new Dialog(mContext);
			dialog.setContentView(R.layout.pop_up);
			dialog.setTitle("Informacion");
			dialog.setCanceledOnTouchOutside(true);

			TextView txtN = (TextView) dialog.findViewById(R.id.txtN);
			//txtN.setText("NOMBRE LUGAR");
			txtN.setText(lugar.getNombre());

			int latitud=(int)(lugar.getLatitud()*1000000);
			int longitud=(int)(lugar.getLongitud()*1000000);
			GeoPoint point = new GeoPoint(latitud, longitud);
			address=getAddress(point);			
			TextView txtA = (TextView) dialog.findViewById(R.id.txtAddress);
			txtA.setText(address);

			ImageView img = (ImageView) dialog.findViewById(R.id.imgPlace);
			Drawable image = ImageOperations(mContext,lugar.getUrlPrincipal(),"image.jpg");			
			img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			img.setImageDrawable(image);

			//TextView txtRank = (TextView) dialog.findViewById(R.id.txtRan);
			//txtN.setText("NOMBRE LUGAR");
			ImageView imgRanking=(ImageView) dialog.findViewById(R.id.imageRanking2);
			int rank=lugar.getRanking();
			switch(rank){
			case 1: imgRanking.setImageResource(R.drawable.ranking1); break;
			case 2: imgRanking.setImageResource(R.drawable.ranking2); break;
			case 3: imgRanking.setImageResource(R.drawable.ranking3); break;
			case 4: imgRanking.setImageResource(R.drawable.ranking4); break;
			default: imgRanking.setImageResource(R.drawable.ranking5); 
			}
			/*set up image view
			ImageView img = (ImageView) dialog.findViewById(R.id.imgPlace);
			img.setImageResource(R.drawable.androidmarker);*/

			//set up buttons
			Button btnDetails = (Button) dialog.findViewById(R.id.btnDetalles);
			btnDetails.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {					
					Intent intent = new Intent(mContext, Pestanias.class);

					//intent.putExtra("id_lugar", lugar.getId());

					intent.putExtra("id_lugar", ""+lugar.getId());
					intent.putExtra("nombre", lugar.getNombre());
					intent.putExtra("direccion", address);
					intent.putExtra("ranking", ""+lugar.getRanking());
					intent.putExtra("descripcion", lugar.getDescripcion());
					intent.putExtra("url", lugar.getUrlPrincipal());

					mContext.startActivity(intent);
					
					dialog.cancel();
				}
			});
			Button btnRoute = (Button) dialog.findViewById(R.id.btnRoute);
			btnRoute.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {					
					Route r=new Route(currentPosPoint, tapPoint, j,mv);
					r.DrawPath();				
					dialog.cancel();
				}
			});
			dialog.show();
		}
		else{
			Dialog dialog = new Dialog(mContext);
			dialog.setContentView(R.layout.pop_up_currentpos);
			dialog.setTitle("Informacion");		
			dialog.setCanceledOnTouchOutside(true);
			TextView txtN = (TextView) dialog.findViewById(R.id.txtN);
			txtN.setText("POSICION ACTUAL");
			String addr=getAddress(tapPoint);
			TextView txtA = (TextView) dialog.findViewById(R.id.txtAddress);
			txtA.setText(addr);
			dialog.show();			
		}
		return(true);
	}

	public String getAddress(GeoPoint p)
	{
		String add="";
		Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());	         
		try {
			List<Address> addresses = geocoder.getFromLocation(
					p.getLatitudeE6()  / 1E6, 
					p.getLongitudeE6() / 1E6, 1);
			if (addresses.size() > 0) 
			{
				for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
					add += addresses.get(0).getAddressLine(i) + "\n";
			}			
		}
		catch (IOException e) {   			
			e.printStackTrace();
		}   

		return add;
	}





	private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}


	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}


	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);		
		populate();
	}

	public void addOverlay(int i, OverlayItem overlay) {
		mOverlays.add(i, overlay);		
		populate();
	}

	public void addOverlayPlace(Lugar lugar) {
		mOverlaysLugar.add(lugar);		
	}

	public void addOverlayPlace(int i, Lugar lugar) {		
		mOverlaysLugar.add(i,lugar);		
	}

	public void removeOverlay(OverlayItem overlay) {
		mOverlays.remove(overlay);	   
		populate();
	}

	public void removeOverlays() {
		int s=mOverlays.size();	
		for(int i=s-1;i>0;i--){			
			mOverlays.remove(i);
			mOverlaysLugar.remove(i);			
		}	   
		populate();
	}
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
}
